package com.aier.classes.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.classes.common.InviteRequestUtil;
import com.aier.classes.entity.RequestParent;
import com.aier.common.common.SystemDateFormatUtil;


public class RequestParentRowMapper implements RowMapper<RequestParent> {

    private int type;
    
    public static final int AND_PARENTNAMES = 1;

    public RequestParentRowMapper() {
    }

    public RequestParentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public RequestParent mapRow(ResultSet rs, int rowNum) throws SQLException {
    	RequestParent rp = parseRequestParent(new RequestParent(), rs);
    	rp.setCtime_str(SystemDateFormatUtil.formatDate(rp.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
    	rp.setState_str(InviteRequestUtil.RequestStateMap.get(rp.getState()));
    	
        switch (type) {
            case AND_PARENTNAMES:
            	rp.setName_parent(rs.getString("name_parent"));
            	rp.setName_student(rs.getString("name_student"));
            	break;
        }
        return rp;
    }

    public static RequestParent parseRequestParent(RequestParent s, ResultSet rs) throws SQLException {
        return parseRequestParent(s, rs, null);
    }

    public static RequestParent parseRequestParent(RequestParent s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setRpid(rs.getLong(su+"rpid"));
        } catch (SQLException e) {
            s.setRpid(null);
        }
        try {
            s.setParent_uid(rs.getLong(su+"parent_uid"));
        } catch (SQLException e) {
            s.setParent_uid(null);
        }
        try {
            s.setBaby_uid(rs.getLong(su+"baby_uid"));
        } catch (SQLException e) {
            s.setBaby_uid(null);
        }
        try {
            s.setRelation(rs.getString(su+"relation"));
        } catch (SQLException e) {
            s.setRelation(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setContent(rs.getString(su+"content"));
        } catch (SQLException e) {
            s.setContent(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, RequestParent s) {
    	par.put("rpid",s.getRpid());
    	par.put("parent_uid",s.getParent_uid());
        par.put("baby_uid",s.getBaby_uid());
        par.put("relation",s.getRelation());
        par.put("tid",s.getTid());
        par.put("cid",s.getCid());
        par.put("content",s.getContent());
        par.put("state",s.getState());
        par.put("ctime",s.getCtime());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, RequestParent s) {
        args.put("rpid",s.getRpid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final RequestParent s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "rpid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(RequestParent s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into request_parent(");
        int count = 0;
        if (null != s.getParent_uid()) {
            sql.append("parent_uid,");
            count++;
        }
        if (null !=s.getBaby_uid()) {
            sql.append("baby_uid,");
            count++;
        }
        if (null != s.getRelation()) {
            sql.append("relation,");
            count++;
        }
        if (null != s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null != s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null != s.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        if (null != s.getName_parent()) {
            sql.append("name_parent,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, RequestParent s)
        throws SQLException {
        int count = 1;
        if (null != s.getParent_uid()) {
            ps.setLong(count++,s.getParent_uid());
        }
        if (null != s.getBaby_uid()) {
            ps.setLong(count++, s.getBaby_uid());
        }
        if (null != s.getRelation()) {
            ps.setString(count++,s.getRelation());
        }
        if (null != s.getTid()) {
            ps.setLong(count++,s.getTid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++,s.getCid());
        }
        if (null != s.getContent()) {
            ps.setString(count++,s.getContent());
        }
        if (null != s.getState()) {
            ps.setShort(count++,s.getState());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        if (null != s.getName_parent()) {
            ps.setString(count++,s.getName_parent());
        }

    }

}
